<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MQTT Client</title>
    <script src="http://cdn.staticfile.net/mqtt/5.3.4/mqtt.js"></script>
</head>
<body>
    <h1>MQTT Client</h1>
    <button id="connect">Connect</button>
    <button id="disconnect" disabled>Disconnect</button>
    <input type="text" id="topic" placeholder="Enter topic">
    <input type="text" id="message" placeholder="Enter message">
    <button id="publish">Publish</button>
    <input type="text" id="subscribeTopic" placeholder="Enter subscription topic">
    <button id="subscribe">Subscribe</button>
    <div id="log"></div>

    <script>
        const client = {
            connected: false,
            connectionOptions: {
                onSuccess: function () {
                    console.log('Connected to MQTT broker.');
                    document.getElementById('disconnect').disabled = false;
                    document.getElementById('connect').disabled = true;
                },
                onFailure: function (message) {
                    console.log('Connection failed: ' + message);
                },
                timeout: 3,
                keepAliveInterval: 60,
                cleanSession: true,
                useSSL: true,
                inviteReconnect: true,
                reconnect: true,
                onReconnect: function () {
                    console.log('Reconnected to MQTT broker.');
                },
                willMessage: {
                    topic: 'client/lastWill',
                    payload: 'Connection Closed',
                    qos: 0,
                    retain: false
                }
            },
            onMessageArrived: function (message) {
                console.log('Message arrived.');
                console.log(message.destinationName, message.payloadString);
                document.getElementById('log').innerHTML += `<p>${message.destinationName}: ${message.payloadString}</p>`;
            }
        };

        document.getElementById('connect').addEventListener('click', function () {
            client.client = new Paho.MQTT.Client('broker.hivemq.com', Number(8000), 'mqttjs_' + parseInt(Math.random() * 100, 10));
            client.client.onConnectionLost = function (responseObject) {
                if (responseObject.errorCode !== 0) {
                    console.log('Connection lost: ' + responseObject.errorMessage);
                    client.connected = false;
                    document.getElementById('disconnect').disabled = true;
                    document.getElementById('connect').disabled = false;
                }
            };
            client.client.onMessageArrived = client.onMessageArrived;
            client.client.connect(client.connectionOptions);
            client.connected = true;
        });

        document.getElementById('disconnect').addEventListener('click', function () {
            client.client.disconnect();
            client.connected = false;
            document.getElementById('disconnect').disabled = true;
            document.getElementById('connect').disabled = false;
        });

        document.getElementById('publish').addEventListener('click', function () {
            const topic = document.getElementById('topic').value;
            const message = document.getElementById('message').value;
            if (client.connected) {
                client.client.send(topic, message);
            }
        });

        document.getElementById('subscribe').addEventListener('click', function () {
            const topic = document.getElementById('subscribeTopic').value;
            if (client.connected) {
                client.client.subscribe(topic, { qos: 0 });
            }
        });
    </script>
</body>
</html>